package com.ruoyi.business.controller;

import java.util.List;

import com.ruoyi.business.utils.SnGenerateUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.JtPurchaseInboundOrder;
import com.ruoyi.business.service.IJtPurchaseInboundOrderService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 采购入库单Controller
 * 
 * @author Alan Zhang
 * @date 2025-03-29
 */
@Controller
@RequestMapping("/business/purchase/inbound")
public class JtPurchaseInboundOrderController extends BaseController
{
    private String prefix = "business/purchase/inbound";

    @Autowired
    private IJtPurchaseInboundOrderService jtPurchaseInboundOrderService;

    @RequiresPermissions("purchase:inbound:view")
    @GetMapping()
    public String inbound()
    {
        return prefix + "/inbound";
    }

    /**
     * 查询采购入库单列表
     */
    @RequiresPermissions("purchase:inbound:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(JtPurchaseInboundOrder jtPurchaseInboundOrder)
    {
        startPage();
        List<JtPurchaseInboundOrder> list = jtPurchaseInboundOrderService.selectJtPurchaseInboundOrderList(jtPurchaseInboundOrder);
        return getDataTable(list);
    }

    /**
     * 导出采购入库单列表
     */
    @RequiresPermissions("purchase:inbound:export")
    @Log(title = "采购入库单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(JtPurchaseInboundOrder jtPurchaseInboundOrder)
    {
        List<JtPurchaseInboundOrder> list = jtPurchaseInboundOrderService.selectJtPurchaseInboundOrderList(jtPurchaseInboundOrder);
        ExcelUtil<JtPurchaseInboundOrder> util = new ExcelUtil<JtPurchaseInboundOrder>(JtPurchaseInboundOrder.class);
        return util.exportExcel(list, "采购入库单数据");
    }

    /**
     * 新增采购入库单
     */
    @RequiresPermissions("purchase:inbound:add")
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("inboundOrderNo", SnGenerateUtil.inboundOrderNumber());
        return prefix + "/add";
    }

    /**
     * 新增保存采购入库单
     */
    @RequiresPermissions("purchase:inbound:add")
    @Log(title = "采购入库单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(JtPurchaseInboundOrder jtPurchaseInboundOrder)
    {
        return toAjax(jtPurchaseInboundOrderService.insertJtPurchaseInboundOrder(jtPurchaseInboundOrder));
    }

    /**
     * 修改采购入库单
     */
    @RequiresPermissions("purchase:inbound:edit")
    @GetMapping("/edit/{inboundOrderId}")
    public String edit(@PathVariable("inboundOrderId") Long inboundOrderId, ModelMap mmap)
    {
        JtPurchaseInboundOrder jtPurchaseInboundOrder = jtPurchaseInboundOrderService.selectJtPurchaseInboundOrderByInboundOrderId(inboundOrderId);
        mmap.put("jtPurchaseInboundOrder", jtPurchaseInboundOrder);
        return prefix + "/edit";
    }

    /**
     * 修改保存采购入库单
     */
    @RequiresPermissions("purchase:inbound:edit")
    @Log(title = "采购入库单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(JtPurchaseInboundOrder jtPurchaseInboundOrder, String removeItemIds)
    {
        return toAjax(jtPurchaseInboundOrderService.updateJtPurchaseInboundOrder(jtPurchaseInboundOrder, removeItemIds));
    }

    /**
     * 删除采购入库单
     */
    @RequiresPermissions("purchase:inbound:remove")
    @Log(title = "采购入库单", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(jtPurchaseInboundOrderService.deleteJtPurchaseInboundOrderByInboundOrderIds(ids));
    }


    /**
     * 新增保存采购入库单
     */
    @RequiresPermissions("purchase:inbound:add")
    @Log(title = "采购入库单-生成", businessType = BusinessType.INSERT)
    @PostMapping("/gen")
    @ResponseBody
    public AjaxResult genSave(String ids)
    {
        return jtPurchaseInboundOrderService.genJtPurchaseInboundOrder(ids);
    }


    /**
     * 入库 采购入库单
     */
    @RequiresPermissions("purchase:inbound:add")
    @Log(title = "采购入库单-入库", businessType = BusinessType.INSERT)
    @PostMapping("/inbound")
    @ResponseBody
    public AjaxResult inbound(Long inboundOrderId)
    {
        return jtPurchaseInboundOrderService.doInbound(inboundOrderId);
    }



    /**
     * 取消 采购入库单
     */
    @RequiresPermissions("purchase:inbound:add")
    @Log(title = "采购入库单-取消", businessType = BusinessType.INSERT)
    @PostMapping("/cancel")
    @ResponseBody
    public AjaxResult cancelInbound(String ids)
    {
        return toAjax(jtPurchaseInboundOrderService.updateJtPurchaseInboundOrderCancelStatus(ids));
    }
}
